package m1;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        // 创建消费者
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("c2");
        // 设置 name server
        c.setNamesrvAddr("192.168.64.141:9876");
        // 订阅消息
        c.subscribe("Topic1", "Tag1");
        // 用来处理消息的监听器
        // Concurrently:并发,消费者启动多个线程,同时接收处理消息
        c.setMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            for (MessageExt ext : msgs) {
                String s = new String(ext.getBody());
                System.out.println("收到:" + s);
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

            // 处理消息失败时,要返回这个状态
            // 通知服务器,稍后重新投递这条消息(消息重试)
            //return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            //稍后重新消费
        });
        // 启动,连接服务器,开始接收消息
        c.start();
    }
}
